package org.raymond.icloud.common.starter.autoconfiguration;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import lombok.extern.slf4j.Slf4j;
import org.raymond.icloud.common.core.config.FeignConfiguration;
import org.raymond.icloud.common.core.mybatis.OrderItemEditor;
import org.raymond.icloud.common.starter.webFilter.*;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.support.FormattingConversionService;
import org.springframework.validation.Validator;
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;

@Slf4j
@Configuration
@AutoConfigureBefore(FeignConfiguration.class)
public class FilterConfiguration {

    @Bean
    public FilterRegistrationBean dataReusableFilter(){
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new DataReusableFilter());
        registration.addUrlPatterns("/*");
        registration.setName("dataReusableFilter");
        registration.setOrder(0);
        return registration;
    }

    @Bean
    public FilterRegistrationBean securityFilter(){
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new SecurityFilter());
        registration.addUrlPatterns("/*");
        registration.setName("securityFilter");
        registration.setOrder(1);
        return registration;
    }

    @Bean
    public FilterRegistrationBean loginFilter(){
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new LoginFilter());
        registration.addUrlPatterns("/*");
        registration.setName("loginFilter");
        registration.setOrder(7);
        return registration;
    }

    @Bean
    public FilterRegistrationBean authenticationFilter(){
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new AuthenticationFilter());
        registration.addUrlPatterns("/*");
        registration.setName("authenticationFilter");
        registration.setOrder(8);
        return registration;
    }

    public FilterRegistrationBean repeatSubmitFilter(){
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new RepeatSubmitFilter());
        registration.addUrlPatterns("/*");
        registration.setName("repeatSubmitFilter");
        registration.setOrder(9);
        return registration;
    }

    public FilterRegistrationBean limitFlowFilter(){
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new LimitFlowFilter());
        registration.addUrlPatterns("/*");
        registration.setName("limitFlowFilter");
        registration.setOrder(10);
        return registration;
    }

    public FilterRegistrationBean logFilterBean(){
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new LogFilter());
        registration.addUrlPatterns("/*");
        registration.setName("logFilterBean");
        registration.setOrder(50);
        return registration;
    }

    @Bean
    public ConfigurableWebBindingInitializer configurableWebBindingInitializer(FormattingConversionService mvcConversionService, Validator mvcValidator) {
        ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
        initializer.setConversionService(mvcConversionService);
        initializer.setValidator(mvcValidator);
        //装配自定义属性编辑器
        initializer.setPropertyEditorRegistrar(propertyEditorRegistry -> {
            //PropertyEditors并不是线程安全的，对于每一个请求，我们都需要new一个PropertyEditor对象
            propertyEditorRegistry.registerCustomEditor(OrderItem.class, new OrderItemEditor());
        });
        return initializer;
    }
}
